package ru.nsu.fit.client.core.soap.registration;

import org.ksoap2.serialization.SoapObject;
import ru.nsu.fit.client.core.soap.SOAPExecutor;

import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;

public class Registration {
    private static final String WSDL_NAME = "registration";
    private static final String METHOD_NAME = "registerUser";
    private static final String NAMESPACE = "http://registration.ws.fit.nsu.ru/";
	
	public static RegistrationOutput execute(final String serverLocation, final String username, final String password) throws Exception
	{
		String wsdlLocation = serverLocation + WSDL_NAME + "?wsdl";
		
		Set<Entry<String, String>> params = new HashSet<Entry<String,String>>();
		params.add(new AbstractMap.SimpleEntry<String, String>("login", username));
		params.add(new AbstractMap.SimpleEntry<String, String>("password", password));
		
		SoapObject response = SOAPExecutor.executeSOAP(wsdlLocation, NAMESPACE, METHOD_NAME, params);
			
		RegistrationOutput out = new RegistrationOutput();
		out.setSuccessful(Boolean.parseBoolean(response.getPropertyAsString("successful")));
		if (response.hasProperty("errorMessage"))
		{
			out.setErrorMessage(response.getPropertyAsString("errorMessage"));
		}
		
		return out;
	}
}
